package structure.MyBinarySearchTree;
class Node{
    public int val;
    public Node left;
    public Node right;
    public Node(int val){
        this.val = val;
    }
}

public class BinarySearchTree {
    public Node root = null;

    /**
     * 查询二叉搜索树的某个节点是否存在
     * @param key 要查找的节点值
     * @return 节点
     */
    public Node searchKey(int key){
        Node cur = root;
        while (cur != null){
            if (key < cur.val){
                cur = cur.left;
            }else if(key > cur.val){
                cur = cur.right;
            }else{
                return cur;
            }
        }
        return null;
    }

    /**
     * 插入某个节点
     * @param val 节点的值
     * @return 是否插入成功
     */
    public boolean insert(int val){
        if (root == null){
            root = new Node(val);
            return true;
        }
        Node parent = null;
        Node cur = root;
        while (cur != null){
            if (val < cur.val){
                parent = cur;
                cur = cur.left;
            }else if(val > cur.val){
                parent = cur;
                cur = cur.right;
            }else{
                return false;
            }
        }
        if (val < parent.val){
            parent.left = new Node(val);
        }else{
            parent.right = new Node(val);
        }
        return true;
    }

    /**
     * 删除某个节点
     * @param key 要删除的节点值
     */
    public boolean remove(int key){
        Node cur = root;
        Node parent = null;
        if (root == null) return false;
        while (cur != null){
            if (cur.val == key){
                removeNode(cur,parent);
                return true;
            }else if (cur.val < key){
                parent = cur;
                cur = cur.right;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        return false;
    }

    public void removeNode(Node cur,Node parent) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        }else if (cur.right == null){
            if (cur == root){
                root = cur.left;
            }else if(cur == parent.left){
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }else{
            Node ta = cur.right;
            Node tap = cur;
            while (ta.left != null){
                tap = ta;
                ta = ta.left;
            }
            cur.val = ta.val;
            removeNode(ta,tap);
        }
    }

    public void inOrder(Node root){
        if (root == null) return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    public static void main(String[] args) {
        int[] array = {10,8,19,3,9,4,7};
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        for (int i = 0; i < array.length; i++) {
            binarySearchTree.insert(array[i]);
        }
        binarySearchTree.inOrder(binarySearchTree.root);
        //System.out.println(binarySearchTree.insert(3));
        binarySearchTree.remove(19);
        System.out.println();
        binarySearchTree.inOrder(binarySearchTree.root);
    }



}
